package com.wisdytech.common.websocket.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.websocket.handler.WebSocketPushHandler;
import com.wisdytech.linkdcs.configuration.dto.ConfigurationEqDto;
import com.wisdytech.linkdcs.configuration.service.ITtEqConfigurationDataService;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipAlarmParamDao;
import com.wisdytech.linkdcs.equipment.dto.EquipAlarmDataDto;
import com.wisdytech.linkdcs.equipment.model.AlarmRecord;
import com.wisdytech.linkdcs.equipment.model.EquipAlarmParam;
import com.wisdytech.linkdcs.equipment.service.IEquipmentAlarmService;

/**
 * 组态定时推送任务
 * @author dlb
 *
 */
@Component
public class ConfigurationQuartzService {
	
	@Autowired
	private ITtEqConfigurationDataService configurationDataService;
	
	@Resource
    private IEquipmentAlarmService equipmentAlarmService;
	
	@Autowired
	IEqEquipAlarmParamDao iEqEquipAlarmParamDao;
	
	/**
	 * 定时推送websocket设备参数实时数据
	 * 每分钟推送一次
	 */
	@Scheduled(cron="0/25 * * * * ?")
	public void sendEqParamValue() {
		//获取缓存的session
		Set<Entry<String, WebSocketSession>> entrys =
				WebSocketPushHandler.userSessionLists.entrySet();
		
		for(Entry<String, WebSocketSession> entry : entrys) {
			
			WebSocketSession session =entry.getValue();
			String configurationId =(String) session.getAttributes().get("zutaiId");
			
			List<ConfigurationEqDto>  eqDtos=
					configurationDataService.getEquipmentRealData(configurationId);
			configurationDataService.pushEquipmentData(eqDtos, session);;
			
		}
		
	}
	
	/**
	 * 定时推送告警信息
	 */

	/*@Scheduled(cron="0/25 * * * * ?")*/
	public void sendAlarmParamValue() {
		
		Filter filter =new Filter();
		filter.setNum(1);
		filter.setSize(1000000);
		PageInfo<AlarmRecord> pageInfo=
                equipmentAlarmService.listAlarmRecord(filter, "", "", "");
        List<AlarmRecord> lists=pageInfo.getList();
        
        for(AlarmRecord ar : lists) {
        	
        	EquipAlarmParam ap =
        			iEqEquipAlarmParamDao.selectById(ar.getAlarmParamId());
        	
        	/*Map<String, Object> alarmMsg=new HashMap<>();
        	alarmMsg.put("equipId", ar.getEqId());
        	alarmMsg.put("paramName", ar.getParamName());
        	alarmMsg.put("paramRange", "大于"+ap.getMinimaValue() +",小于"+ap.getMaximaValue());
        	alarmMsg.put("time", ar.getCreateDate());*/
        	
        	EquipAlarmDataDto  alarmDto =new EquipAlarmDataDto();
        	alarmDto.setEquipId(ar.getEqId());
        	alarmDto.setEquipNo(ar.getEqCode());
        	alarmDto.setParamName(ar.getParamName());
        	alarmDto.setParamRange("大于"+ap.getMinimaValue() +",小于"+ap.getMaximaValue());
        	alarmDto.setValue(ar.getErrorValue());
        	
        	List<EquipAlarmDataDto> alarmMsgs=new ArrayList<>();
        	alarmMsgs.add(alarmDto);
    		
    		//告警信息推送
    		//获取缓存的session
    		Set<Entry<String, WebSocketSession>> entrys =
    				WebSocketPushHandler.userSessionLists.entrySet();
    		
    		for(Entry<String, WebSocketSession> entry : entrys) {
    			
    			WebSocketSession session =entry.getValue();
    			configurationDataService.pushAlarmEquipmentData(alarmMsgs, session);
    			
    		}
    		
    		try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		
		
		
	}
	
	
}
